package com.lixh.virtual.ui.appList.adapter;

import android.view.View;
import android.widget.ImageView;
import android.widget.PopupWindow;
import android.widget.TextView;

import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.lixh.virtual.R;
import com.lixh.virtual.app.os.bean.AppInfo;
import com.lixh.virtual.views.LabelView;
import com.lixh.virtual.views.LauncherIconView;

import java.util.ArrayList;

public class AppListAdapter extends BaseQuickAdapter<AppInfo, BaseViewHolder> {
    private ArrayList<Integer> mSelectedIndices;

    public AppListAdapter() {
        super(R.layout.item_clone_app, new ArrayList<>());
        mSelectedIndices = new ArrayList<>();
    }

    @Override
    protected void convert(BaseViewHolder holder, AppInfo data) {
        ImageView iconView = holder.getView(R.id.item_app_icon);
        TextView nameView = holder.getView(R.id.item_app_name);
        ImageView appCheckView = holder.getView(R.id.item_app_checked);
        LabelView labelView = holder.getView(R.id.item_app_clone_count);
        ImageView isHookIcon = holder.getView(R.id.item_app_ishook);
        iconView.setImageDrawable(data.icon);
        nameView.setText(data.name);
        if (isIndexSelected(holder.getPosition())) {
            iconView.setAlpha(1f);
            appCheckView.setImageResource(R.drawable.ic_check);
        } else {
            iconView.setAlpha(0.65f);
            appCheckView.setImageResource(R.drawable.ic_no_check);
        }

        if (data.cloneCount > 0) {
            labelView.setVisibility(View.VISIBLE);
            labelView.setText(data.cloneCount + 1 + "");
        } else {
            labelView.setVisibility(View.INVISIBLE);
        }
        if (data.isHook) {
            isHookIcon.setVisibility(View.VISIBLE);
        } else {
            isHookIcon.setVisibility(View.INVISIBLE);
        }

    }


    public final int getSelectedCount() {
        return mSelectedIndices.size();
    }

    public final Integer[] getSelectedIndices() {
        return mSelectedIndices.toArray(new Integer[mSelectedIndices.size()]);
    }


    public final boolean isIndexSelected(int index) {
        return mSelectedIndices.contains(index);
    }

    public void toggleSelected(int position) {
        if (!isIndexSelected(position)) {
            mSelectedIndices.add(position);
        } else {
            mSelectedIndices.remove(position);
        }
        notifyItemChanged(position);
    }
}